/*
 * Filename:    WorkFlowServiceImpl.java
 * Copyright:   2014 中国平安保险（集团）股份有限公司 版权所有
 * Company:     平安科技养老险及健康险系统开发部
 * Create at:   2014年10月29日
 */
package com.paic.mhis.hcpms.warning.biz.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.paic.mhis.core.auth.biz.service.Authority;
import com.paic.mhis.core.auth.biz.service.UserDetails;
import com.paic.mhis.hcpms.common.workflow.biz.service.HcpmsWFService;
import com.paic.mhis.hcpms.common.workflow.vo.HcpmsWorkFlowVO;
import com.paic.mhis.hcpms.warning.biz.service.WarningWorkFlowService;
import com.paic.mhis.hcpms.warning.util.WarnConstants;

@Component("warningWorkFlowService")
public class WarningWorkFlowServiceImpl implements WarningWorkFlowService {
	@Autowired
	private HcpmsWFService hcpmsWFService;

	@Override
	public String startWorkFlow(String warnNo, UserDetails user, Map<String, String> condition) throws Exception {
		HcpmsWorkFlowVO vo = new HcpmsWorkFlowVO();
		if (user != null) {
			List<Authority> authorityList = user.getAuthorities();
			List<String> role = new ArrayList<String>();
			for (Authority authority : authorityList) {
				role.add(authority.getAuthority());
			}
			vo.setRole(role);
			vo.setActCurrent(user.getUsername());
			vo.setAssigner(user.getUsername());
		}
		vo.setTaskNo(warnNo);
		vo.setTaskType(WarnConstants.TASK_TYPE);
		vo.setParamValue(condition);
		return hcpmsWFService.startProcess(vo).getActCurrent();
	}

	@Override
	public String processWorkFlow(HcpmsWorkFlowVO vo) throws Exception {
		vo.setTaskType(WarnConstants.TASK_TYPE);
		return hcpmsWFService.processFlow(vo).getActCurrent();
	}

	@Override
	public String vilidateOrBindUser(String warnNo, String username, String status) {
		if (StringUtils.isBlank(warnNo) || StringUtils.isBlank(username) || StringUtils.isBlank(status)) {
			return WarnConstants.PROCESS_OUT_POWER;
		}
		HcpmsWorkFlowVO vo = new HcpmsWorkFlowVO();
		vo.setTaskNo(warnNo);
		vo.setActCurrent(username);
		vo.setActNo(status);
		vo.setTaskType(WarnConstants.TASK_TYPE);
		String processState = hcpmsWFService.vilidateOrBindUser(vo);
		return processState;
	}

}
